package cn.tedu.nybikeT.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.tedu.nybikeT.mapper.OperationMapper;
import cn.tedu.nybikeT.pojo.OperationDO;
import cn.tedu.nybikeT.pojo.OperationVO;
import cn.tedu.nybikeT.pojo.OptTypeCountDO;
import cn.tedu.nybikeT.pojo.StationClickCountDO;
import cn.tedu.nybikeT.pojo.UserIPCountDO;
import cn.tedu.nybikeT.service.IOperationService;
import cn.tedu.nybikeT.service.ex.EmptyArgumentException;
import cn.tedu.nybikeT.service.ex.InsertException;

@Service
public class OperationServiceImpl implements IOperationService {
	
	@Autowired
	OperationMapper mapper;
	
	private String[] optTypeArr= {"综合","拖拽","点击","缩放"};

	@Override
	public OperationVO findOperationData(String date, int stationCountLimit, int ipLimit) {
		// 调用持久层方法查询数据
		List<OptTypeCountDO> otcList=mapper.listOptTypeCount(date);
		List<StationClickCountDO> sccList=mapper.listStationClickCount(date, stationCountLimit);
		List<UserIPCountDO> uicList=mapper.listUserIPCount(date, ipLimit);
		// 声明目标集合
		List<Integer> accessCountXData=new ArrayList<Integer>(otcList.size()+1);
		List<String> accessCountYData=new ArrayList<String>(otcList.size()+1);
		List<Integer> stationCountXData=new ArrayList<Integer>(stationCountLimit);
		List<String> stationCountNameData=new ArrayList<String>(stationCountLimit);
		List<Integer> stationCountYData=new ArrayList<Integer>(stationCountLimit);
		List<String> ipCountXData=new ArrayList<String>(ipLimit);
		List<Integer> ipCountYData=new ArrayList<Integer>(ipLimit);
		// 遍历数据封装到目标集合中
		Integer tempCount=0;
		for(OptTypeCountDO otc:otcList) {
			accessCountXData.add(otc.getCount());
			accessCountYData.add(optTypeArr[otc.getOptType()]);
			tempCount+=otc.getCount();
		}
		// 添加综合操作数据
		accessCountXData.add(tempCount);
		accessCountYData.add(optTypeArr[0]);
		
		for(StationClickCountDO scc:sccList) {
			stationCountXData.add(scc.getStationId());
			stationCountNameData.add(scc.getName());
			stationCountYData.add(scc.getCount());
		}
		
		for(UserIPCountDO uic:uicList) {
			ipCountXData.add(uic.getUserIP());
			ipCountYData.add(uic.getCount());
		}
		// 创建VO对象，将集合封装到VO中
		OperationVO vo=new OperationVO(accessCountXData, accessCountYData, 
				stationCountXData, stationCountNameData, 
				stationCountYData, ipCountXData, ipCountYData);
		// 返回VO对象
		return vo;
	}

	@Override
	public void saveOpeartion(OperationDO opt) {
		// 非空验证
		if(opt==null || StringUtils.isEmpty(opt.getUserIp()) || opt.getOptType()==null) {
			throw new EmptyArgumentException("添加操作记录异常：操作记录为空");
		}
		if(opt.getOptType().equals(1)) {// 拖拽操作
			if(opt.getDragStartLat()==null || opt.getDragStartLon()==null || opt.getDragEndLat()==null || opt.getDragEndLon()==null) {
				throw new EmptyArgumentException("添加操作记录异常：拖拽记录为空");
			}
		}
		if(opt.getOptType().equals(2)) {
			if(opt.getStationId()==null) {
				throw new EmptyArgumentException("添加操作记录异常：点击记录为空");
			}
		}
		if(opt.getOptType().equals(3)) {
			if(opt.getZoomStartLevel()==null || opt.getZoomEndLevel()==null) {
				throw new EmptyArgumentException("添加操作记录异常：缩放记录为空");
			}
		}
		// 格式验证 TODO
		// 业务逻辑验证 TODO
		// 调用持久层方法，将数据写入数据库
		Integer row=mapper.insertOperation(opt);
		// 对结果进行判断
		if(row!=1) {
			throw new InsertException("添加操作记录异常：添加失败");
		}
	}


}
